const fs = require('fs')
const CLASS_FILE_PATH = './data/class.json'
const USER_FILE_PATH = './data/user.json'

/**
 * 查找所有用户
 * @param {*} callback 
 */
function findAllUser(callback){
    fs.readFile(USER_FILE_PATH, 'utf8', (err, data) => {
        if (err) {
            return callback(err)
        }
        data = JSON.parse(data)
        callback(null,data.users)
    })
}


/**
 * 根据用户id 查找其所管理的班级
 * @param {uid:string} uid 用户ID
 * @param {err:NodeJS.ErrnoException,data:[]} callback 回调函数
 */
function findClassByUserId(uid, callback) {
    fs.readFile(CLASS_FILE_PATH, 'utf8', (err, data) => {
        if (err) {
            return callback(err)
        }

        data = JSON.parse(data)
        if (uid === '001') {
            // 001 管理员，返回所有班级
            callback(null, data.classes)
        } else {
            let classes = data.classes.filter(item => {
                return item.uid == uid
            })
            callback(null, classes)
        }

    })
}




exports.findClassByUserId = findClassByUserId
exports.findAllUser = findAllUser
